package com.example.dangerouscargodriver.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.adapter.BannerViewAdapter;
import com.example.dangerouscargodriver.entry.BannerInfo;
import com.example.dangerouscargodriver.ui.activity.ShowHtmlActivity;

import java.util.ArrayList;
import java.util.List;

public class BannerView extends LinearLayout implements ViewPager.OnPageChangeListener {
    private LinearLayout mContainer = null;
    private int mPrePosition = 0;
    private ViewPager mViewPager = null;
    private List<ImageView> mImageList = null;
    private List<BannerInfo> mBannerInfo = null;
    private BannerViewAdapter mAdapter = null;
    private Context mContext = null;

    public BannerView(@NonNull Context context, List<BannerInfo> banner) {
        super(context);
        mBannerInfo = banner;
        init(context, null);
    }

    public void init(Context context, AttributeSet attrs) {
        mContext = context;
        View itemLayout = LayoutInflater.from(context).inflate(R.layout.home_banner_layout, this);
        mViewPager = itemLayout.findViewById(R.id.vp_pager);
        mContainer = itemLayout.findViewById(R.id.ll_container);
        mImageList = new ArrayList<>();

        for (int x = 0; x < mBannerInfo.size(); x++) {
            final int item = x;
            ImageView iv = new ImageView(context);
            Glide.with(context).load(mBannerInfo.get(x).getImg()).placeholder(R.drawable.logo).into(iv);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ShowHtmlActivity.class);
                    intent.putExtra("url", mBannerInfo.get(item).getUrl());
                    mContext.startActivity(intent);
                }
            });
            mImageList.add(iv);
            View v = new View(context);
            v.setBackgroundResource(R.drawable.point_normal);
            //有多少张图就放置几个点
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15);
            layoutParams.leftMargin = 30;
            mContainer.addView(v,layoutParams);
        }
        mAdapter = new BannerViewAdapter();
        mAdapter.setImageList(mImageList);
        mViewPager.setAdapter(mAdapter);
        mContainer.getChildAt(0).setBackgroundResource(R.drawable.point_select);
        mPrePosition = 0;
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position >= mImageList.size()){
            return;
        }
        int newPosition = position % mImageList.size();
        mContainer.getChildAt(newPosition).setBackgroundResource(R.drawable.point_select);
        if(newPosition != mPrePosition) {
            mContainer.getChildAt(mPrePosition).setBackgroundResource(R.drawable.point_normal);
            mPrePosition = newPosition;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}
